<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-1
 * Time: 下午6:00
 * To change this template use File | Settings | File Templates.
 */
class image_crawl extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        ini_set('memory_limit','3072M');
        set_time_limit(0);
    }

    public function index()
    {
        $limit = 100;
        $start = 0;
        $end   = 400000;

        $this->load->model('movie');
        $this->load->helper('crawl_tools');
        $config = array('dir' => '/data/www/dy1010/web/html/images/', 'crawl_type' => 'image');

        //$url = 'http://www.2tu.cc/Html/GP%d.html';

        for ($i = $start; $i < $end; $i = $i + $limit)
        {
            $crawl = new crawl_tools($config);
            //echo $i."\n";
            //echo $i+$limit."\n";
            $urlArray = array();

            echo $limit.'--'.$i."\n";//continue;
            $field = 'id, img_addr';
            $data = $this->movie->getMovieList($field, $limit, $i);
            //print_r($data);exit;

            foreach ($data as $v)
            {
                if (empty ($v['img_addr'])) continue;

                //* 抓取漏抓的页面
                $fileName = $config['dir'].intToPath($v['id']).$v['id'].'.jpg';
                //echo $fileName;exit;
                if (file_exists(($fileName)) && filesize($fileName) > 3500) continue;
                //*/

        	    $urlArray[$v['id']] = $v['img_addr'];//sprintf($url, $ii);
            }
            //echo '<pre>';print_r($urlArray);exit;//continue;

            //*抓取漏抓的页面
            if (empty ($urlArray)) continue;
            //*/

            $crawl->crawlList($urlArray);
            unset ($crawl);
        }
    }

    /**
     * 抓取更新的图片
     */
    public function crawl_update()
    {
        $this->load->model('movie');
        $this->load->helper('crawl_tools');
        $config = array('dir' => '/data/www/dy1010/web/html/images/', 'crawl_type' => 'image');

        $crawl = new crawl_tools($config);
        //echo $i."\n";
        //echo $i+$limit."\n";
        $urlArray = array();

        $field = 'id, img_addr';
        $data = $this->movie->getMovieList($field, 80, 0, null, 'update_time desc');
//print_r($data);exit;
        foreach ($data as $v)
        {
            if (empty ($v['img_addr'])) continue;

            //* 抓取漏抓的页面
            $fileName = $config['dir'].intToPath($v['id']).$v['id'].'.jpg';
            //echo $fileName;exit;
            if (file_exists(($fileName)) && filesize($fileName) > 3500) continue;
            //*/

    	    $urlArray[$v['id']] = $v['img_addr'];//sprintf($url, $ii);
        }
        //echo '<pre>';print_r($urlArray);exit;//continue;

        $crawl->crawlList($urlArray);
        unset ($crawl);

        //切图

        $config = array('dir' => '/data/www/dy1010/web/html/images/', 'crawl_type' => 'image');
        //$config = array('dir' => 'D:\wamp\www\dy1010/web/html/images/', 'crawl_type' => 'image');

        foreach ($data as $v)
        {
            $fileName = $config['dir'].intToPath($v['id']).$v['id'].'.jpg';
            echo $v['id']."\n";
            if (!file_exists($fileName) || filesize($fileName) < 3500) continue;

            $path = image_path($v['id']);
            if (!is_dir($path)) {
                rMkDir($path);
            }

            $targetFileName = $path.$v['id'].'.jpg';

            copyImg($fileName, 242, 345, $targetFileName);
        }
    }
}
